﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebAppProject.WCF.webAppService;
using System.ServiceModel;
using System.Diagnostics;

namespace CommonClass
{
    /// <summary>
    /// WCF 代理
    /// </summary>
    public class WebServiceProxy
    {
        #region 变量

        private static WebServiceProxy instance = null;
        private static readonly object padLock = new Object();
        private static WebAppServiceClient proxy = null;

        #endregion

        #region 构造函数

        private WebServiceProxy()
        {
            try
            {
                if (proxy == null)
                {
                    BasicHttpBinding binding = new BasicHttpBinding();
                    binding.MaxReceivedMessageSize = int.MaxValue;

                    EndpointAddress endPoint;
                    if (string.IsNullOrWhiteSpace(WcfConfig.WcfServcieUrl4WebAppService))
                        endPoint = new EndpointAddress("http://localhost/Deploy/Service/WebAppService.svc");
                    else
                        endPoint = new EndpointAddress(WcfConfig.WcfServcieUrl4WebAppService);

                    proxy = new WebAppServiceClient(binding, endPoint);
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                Console.WriteLine(ex.Message);
#endif
                proxy = null;
            }
        }

        #endregion

        #region 属性

        public static WebServiceProxy Instance
        {
            get
            {
                lock (padLock)
                {
                    if (instance == null)
                    {
                        instance = new WebServiceProxy();
                    }
                    return instance;
                }
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// 根据项目类别获取最新版本号
        /// </summary>
        /// <param name="projectType">项目类别 ProjectTypeEnum</param>
        /// <param name="callback"></param>
        /// <param name="userState"></param>
        public void GetLatestVersion(string projectType,
            EventHandler<GetLatestVersionCompletedEventArgs> callback,
            object userState)
        {
            if (proxy != null)
            {
                try
                {
                    proxy.GetLatestVersionCompleted += callback;
                    proxy.GetLatestVersionAsync(projectType, userState);
                }
                catch (Exception ex)
                {
#if DEBUG
                    Debug.WriteLine(ex.Message);
#endif
                }
            }
        }

        /// <summary>
        /// 根据项目类别获取更新文件名称列表
        /// </summary>
        /// <param name="projectType">项目类别 ProjectTypeEnum</param>
        /// <param name="callback"></param>
        /// <param name="userState"></param>
        public void GetUpdateFiles(string projectType,
            EventHandler<GetUpdateFilesCompletedEventArgs> callback,
            object userState)
        {
            if (proxy != null)
            {
                try
                {
                    proxy.GetUpdateFilesCompleted += callback;
                    proxy.GetUpdateFilesAsync(projectType, userState);
                }
                catch (Exception ex)
                {
#if DEBUG
                    Debug.WriteLine(ex.Message);
#endif
                }
            }
        }

        #endregion

        #region 移除事件

        public void RemoveGetLatestVersionCompleted(EventHandler<GetLatestVersionCompletedEventArgs> callback)
        {
            proxy.GetLatestVersionCompleted -= callback;
        }

        public void RemoveGetUpdateFilesCompleted(EventHandler<GetUpdateFilesCompletedEventArgs> callback)
        {
            proxy.GetUpdateFilesCompleted -= callback;
        }

        #endregion
    }
}
